package com.example.rabbitmqboot.consumer;

import com.example.rabbitmqboot.pojo.Student;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class StudentConsumer {

    @RabbitListener(queues = "StudentQueue",containerFactory = "rabbitListenerContainerFactory")
    public void studentMessage(@Payload Student student, @Headers Map<String,Object> headers, Channel channel) throws IOException {
        log.info("学生信息:{}",student);
        Long t = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);

        channel.basicAck(t,false); //应答当前消息
    }
}
